package com.swm.util;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.swm.mapper.StockMapper;
import com.swm.pojo.Stock;
import org.apache.ibatis.session.SqlSession;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author 师文明
 * @data 2024/12/23 下午3:11
 **/
public class getStockYearData {
    //创建一个固定大小的线程池
    static ExecutorService executorService = Executors.newFixedThreadPool(10);

    static SqlSession sqlSession = SqlSessionUtil.openSession();
    static StockMapper mapper = sqlSession.getMapper(StockMapper.class);

    static int num = 0;

    public static List<Stock> getStcokList() {
        List<Stock> stocks = mapper.selectAllStock();
        return stocks;
    }

    public static void main(String[] args) throws Exception {
        System.out.println(new Date());
        //获取所有股票列表
        List<Stock> stcokList = getStcokList();

        for (Stock stock : stcokList) {
            executorService.execute(() -> {
                try {
                    getData(stock.getCode(), stock.getType());
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            });
        }

        // 关闭线程池，以优雅的方式
        executorService.shutdown();
    }

    public static void getData(String code, int type) throws Exception {
        // 连接到新浪财经接口
        String url = "https://push2his.eastmoney.com/api/qt/stock/kline/get?cb=jQuery35104492241501364864_1735955909815&secid=" + type + "." + code + "&ut=fa5fd1943c7b386f172d6893dbfba10b&fields1=f1%2Cf2%2Cf3%2Cf4%2Cf5%2Cf6&fields2=f51%2Cf52%2Cf53%2Cf54%2Cf55%2Cf56%2Cf57%2Cf58%2Cf59%2Cf60%2Cf61&klt=103&fqt=1&beg=0&end=20500101";
        URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
        con.setRequestMethod("GET");
        con.setRequestProperty("Cookie", "qgqp_b_id=488de58836c5f30c189b9ad4c700b9f5; st_si=04137509447995; websitepoptg_api_time=1734056942833; st_asi=delete; HAList=ty-1-603999-%u8BFB%u8005%u4F20%u5A92%2Cty-0-002888-%u60E0%u5A01%u79D1%u6280%2Cty-0-002269-%u7F8E%u90A6%u670D%u9970%2Cty-0-000813-%u5FB7%u5C55%u5065%u5EB7%2Cty-0-002045-%u56FD%u5149%u7535%u5668%2Cty-0-300059-%u4E1C%u65B9%u8D22%u5BCC%2Cty-1-688605-N%u5148%u950B%2Cty-1-000001-%u4E0A%u8BC1%u6307%u6570; st_pvi=62081555482493; st_sp=2024-12-11%2010%3A26%3A42; st_inirUrl=https%3A%2F%2Fwww.baidu.com%2Flink; st_sn=60; st_psi=20241213164756225-113200301201-5528754653");
        // 从数据源获取实时股票数据
        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
        String inputLine;
        StringBuffer response = new StringBuffer();
        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();
        String data = response.toString();
        data = data.split("\\(")[1].split("\\)")[0];
        JSONObject jsonObject = JSONObject.parseObject(data);
        if (jsonObject.get("data") != null) {
            JSONObject jsonObject1 = JSONObject.parseObject(jsonObject.get("data").toString());
            String name = jsonObject1.get("name").toString();
            JSONArray klines = JSONArray.parse(jsonObject1.get("klines").toString());


            //最近几年没有大波动
            int months = klines.size() > 30 ? 30 : klines.size();
            //if(klines.size()>months){
            /*    double max=0;
                double min=1000;
                String maxdate = "";
                String mindate = "";

                for (int i=1;i<months;i++){
                    String[] split = klines.get(klines.size() - i).toString().split(",");
                    max= Math.max(max, Double.parseDouble(split[2]));
                    min= Math.min(min, Double.parseDouble(split[2]));
                    maxdate=max>Double.parseDouble(split[2])?maxdate:split[0];
                    mindate=min<Double.parseDouble(split[2])?mindate:split[0];
                }
                double v = (max - min) / (max + min) * 2;
                //当前股价
                String[] split = klines.get(klines.size() -1).toString().split(",");
                double curPrice = Double.parseDouble(split[2]);
                if(v<0.2&&curPrice< ((max + min) / 2)){
                    System.out.println(name);
                    System.out.println(max);
                    System.out.println(min);
                    System.out.println(maxdate);
                    System.out.println(mindate);
                }*/
            double currentPrice = 0.0;
            boolean isup = true;
            for (int i = 1; i < months; i++) {
                double price = Double.parseDouble(klines.get(klines.size() - i).toString().split(",")[2]);
                if (price > currentPrice) {
                    currentPrice = price;
                } else {
                    isup = false;
                    break;
                }
            }

            if (isup) {
                System.out.println(name);
            }



            /*}else {

            }*/
        }
    }

    public static int not10(int i, JSONArray klines) {
        if (Double.parseDouble(klines.get(klines.size() - 1 - i).toString().split(",")[8]) > 9.8 && Double.parseDouble(klines.get(klines.size() - 1 - i).toString().split(",")[8]) < 11) {
            return not10(i + 1, klines);
        } else {
            return i;
        }
    }

    public static int tennum(JSONArray klines) {
        int num = 0;
        for (int i = 1; i < 10; i++) {
            if (Double.parseDouble(klines.get(klines.size() - i).toString().split(",")[8]) > 9.8) {
                num++;
            }
        }
        return num;
    }

    //判断两个值大小
    public static boolean compare(JSONArray klines, int i) {
        for (int j = 0; j < 5; j++) {
            System.out.println(klines.get(klines.size() - i - j).toString().split(",")[2]);
            System.out.println(klines.get(klines.size() - i).toString().split(",")[2]);
            System.out.println(klines.get(klines.size() - i + j).toString().split(",")[2]);
            if (Double.parseDouble(klines.get(klines.size() - i).toString().split(",")[2]) < Double.parseDouble(klines.get(klines.size() - i - j).toString().split(",")[2]) || Double.parseDouble(klines.get(klines.size() - i).toString().split(",")[2]) < Double.parseDouble(klines.get(klines.size() - i - j).toString().split(",")[2])) {
                return false;
            }
        }
        return true;
    }
}

